AS      = as
LD      = ld
LDFLAGS = -Ttext 0
IMAGE   = boot.img

all: image

boot: boot.s
	as86 -0 -a -o boot.o boot.s
	ld86 -0 -s -o boot boot.o
	dd if=boot of=boot.raw bs=32 skip=1

boot_atat: boot_atat.s
	$(AS) -o boot.o boot_atat.s
	$(LD) $(LDFLAGS) boot.o -o boot
	objcopy -O binary boot boot.raw

head: head.s
	$(AS) -o head.o head.s
	$(LD) $(LDFLAGS) -e startup_32 -x head.o -o head
	nm head > System.map
	objcopy -O binary head head.raw

image0: boot_atat head
	cp boot.raw $(IMAGE)
	cat head.raw >> $(IMAGE)

# virtualbox requires 512 byte alignment
# shell command executes at first of each target, so separate it as a new single target
image: image0
	dd if=/dev/zero bs=1 count=$$(( (512 - $(shell stat -c%s $(IMAGE)) % 512) % 512 )) >> $(IMAGE)

disk: image
	dd if=$(IMAGE) of=/dev/fd0 bs=8192
	sync

bochs: image
	touch bochsrc
	bochs 'boot: floppy' 'floppya: 1_44=$(IMAGE), status=inserted' 'config_interface: wx' 'display_library: wx'

qemu: image
	qemu-system-i386 -fda $(IMAGE)

clean:
	rm -f *~
	rm -f $(IMAGE) System.map boot head *.raw *.o bochsrc

